package cn.mcsys.ticket.service.timer;

import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.viewer.entity.AirTickets;
import cn.mcsys.ticket.dao.AirShowPWDAO;
import cn.mcsys.ticket.entity.AirshowPW;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

public class CheckTicketTimer {

    @Autowired
    private BackService backService;

    @Autowired
    private AirShowPWDAO airShowPWDAO;

    private static Long eticketId = 1l;

    private static Long entityTicketId = 1l;

    public void checkTickets(){
//        if(eticketId > 0 && entityTicketId > 0){
            System.out.println("进入增量的票务定时器------------------------------使用的线程是:"+Thread.currentThread().getName());
//            循环证件票，每10S扫一次
//            电子票的，查1000条出来
            List<AirshowPW> airshowEPWS = airShowPWDAO.selectMaxETickets(eticketId);
            for (AirshowPW airshowPW : airshowEPWS) {
                setAirTicket(airshowPW);
            }
            System.out.println("最大的票务证件票id:"+eticketId);
//            查询实物票的
            List<AirshowPW> airshowTPWS = airShowPWDAO.selectMaxEntityTickets(entityTicketId);
            for (AirshowPW airshowTPW : airshowTPWS) {
                setAirTicket(airshowTPW);
            }
            System.out.println("最大的票务实物票id:"+entityTicketId);
//            for (Long i = eticketId; i < eticketId+1000; i++) {
//                AirshowPW airshowPW = airShowPWDAO.get(i);
//                if(airshowPW != null){
//                    setAirTicket(airshowPW);
//                }
//            }
////            实物票的
//            for (Long i = entityTicketId; i < entityTicketId+1000; i++) {
//                AirshowPW airshowPW = airShowPWDAO.get(i);
//                if(airshowPW != null){
//                    setAirTicket(airshowPW);
//                }
//            }
//        }else{
//            forTickets();
//        }
    }

//
    private void forTickets(){
        int pageNO = 0;
        System.out.println("进入票务定时器------------------------------使用的线程是:"+Thread.currentThread().getName());
        while (true) {
            List<AirshowPW> airshowPWS = airShowPWDAO.selectAll(pageNO);
            if(airshowPWS.size() == 0){
                break;
            }
            pageNO += airshowPWS.size();
            System.out.println("pageNO:"+pageNO);
//            List<AirTickets> airTicketsList = new ArrayList<>();
            for (AirshowPW airshowPW : airshowPWS) {
                setAirTicket(airshowPW);
            }
        }

    }

    private void setAirTicket(AirshowPW airshowPW){
        AirTickets airTickets = new AirTickets();
        String performname = airshowPW.getPerformname();
        String phone = airshowPW.getPhone();
        String productName = airshowPW.getProductName();
        switch (productName) {
            case "个人票（证件票）":
                airTickets.setParType(0);
                break;
            case "长者学生儿童票（证件票）":
                airTickets.setParType(1);
                break;
            case "个人票（实物票）":
                airTickets.setParType(2);
                break;
            case "长者学生儿童票（实物票）":
                airTickets.setParType(3);
                break;
            case "团队票":
                airTickets.setParType(4);
                break;
            case "个人票+动态演示区套票":
                airTickets.setParType(5);
                break;
            case "长者学生儿童票+动态演示区套票":
                airTickets.setParType(6);
        }
        String idcardType = airshowPW.getIdcardType();
        switch (idcardType) {
            case "身份证":
                airTickets.setTicketType(0);
                break;
            case "回乡证":
                airTickets.setTicketType(1);
                break;
            case "台胞证":
                airTickets.setTicketType(2);
                break;
            case "外国人护照":
                airTickets.setTicketType(3);
                break;
            case "实物票":
                airTickets.setTicketType(4);
        }
        String cardID = airshowPW.getCardID();
        airTickets.setPhone(phone);
        airTickets.setTicketDate(performname);
        airTickets.setCreateDate(new Date());
        airTickets.setStatus(0);
        if(airTickets.getTicketType() == 4){
            cardID = airshowPW.getCarcode();
            if(entityTicketId < airshowPW.getId()){
                entityTicketId = airshowPW.getId();
            }
        }else{
            if(eticketId < airshowPW.getId()){
                eticketId = airshowPW.getId();
            }
        }
        airTickets.setTicketNumber(cardID);
        AirTickets alreadyTicket = backService.selectByAirTickets(airTickets);
        if (alreadyTicket == null) {
            backService.saveTicket(airTickets);
            System.out.println("保存票务信息");
        }
    }
}
